var comPageNum = 1;//分页公共页码
/*查询数据*/
function queryopenScreen(pageNum) {
    comPageNum = pageNum;
    $.ajax({
        url:root.appversioncontrol.readBackScreenManageList,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageSize":"10",
            "pageNum":pageNum,
            "pictureName":$("#pictureName").val(),
            "pictureType":$("#pictureType").val(),
            "status":$("#status").val(),
            "startTime":$("#timemin").val(),
            "endTime":$("#timemax").val(),

        }),
        success: function (data) {
            console.log(data)
            if(data.errorCode==0){
                if(data.data.list.length==0&&comPageNum!=1){
                    queryopenScreen(comPageNum-1);
                    return;
                }
                homePagelist(data.data, 0);
                $('.M-box3').pagination({
                    pageCount:data.data.pages,
                    current:pageNum,
                    jump: true,
                    coping: true,
                    prevContent: '上页',
                    nextContent: '下页',
                    callback: function (api) {
                        comPageNum = api.getCurrent();
                        queryopenScreen(api.getCurrent());
                    }
                });
            }else {
                homePagelist("", 0);
            }
            /*$('.M-box3').pagination({
                pageCount: data.data.pages,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url:root.appversioncontrol.readBackScreenManageList,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageSize":"10",
                            "pageNum":api.getCurrent(),
                            "pictureName":$("#pictureName").val(),
                            "pictureType":$("#pictureType").val(),
                            "status":$("#status").val(),
                            "startTime":$("#timemin").val(),
                            "endTime":$("#timemax").val(),
                        }),
                        success: function (result) {
                            homePagelist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });*/
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
/*表格*/
var dataSource = new Array();
//table的数据源
var table;
dataSource = [];
function homePagelist(data, index){
    dataSource.splice(0,dataSource.length);//清空数组
    var datashu =data.startRow;
    $.each(data.list, function (i, field) {

        var datashus = datashu++
        var rowData = new Array();
        rowData.push(datashus);
        rowData.push(field.pictureName);
        rowData.push(field.pictureType);
        rowData.push(field.startTime);
        rowData.push(field.endTime);
        rowData.push(field.updateTime);
        rowData.push(field.status);
        rowData.push(field.updateUser);
        rowData.push(field.id);
        dataSource.push(rowData);

    });

    initData(dataSource);
    function initData(dataSource) {
        var dttable = $('#openScreentable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#openScreentable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": false,
            "bAutoWidth": false,
            "bLengthChange": false,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var names=""
                        if (row[2] == 1) {
                            names='引导图'
                        }else  if (row[2] == 2) {
                            names=row[1]
                        }else  if (row[2] == 3) {
                            names=row[1]
                        }
                        var val = '<span id="consignee" onmousemove="over(\''+row[8]+'\')" onmouseout="out(\''+row[8]+'\')" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;width:50px;display:inline-block;">'+names+'</span> <div id="mouseBox'+row[8]+'" style="position:absolute;max-width: 163px; background: #fff;padding:10px;border:1px solid #ccc;display:none;">'+names+'</div>';
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[2] == 1) {
                            val = '引导页'
                        }else  if (row[2] == 2) {
                            val = '启动页'
                        }else  if (row[2] == 3) {
                            val = '广告页'
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val=""
                        if(row[3]!="1000-01-01 00:00:00" && row[4]!="1000-01-01 00:00:00"){
                            val = row[3]+'~'+row[4]
                        }else {
                            val = ""
                        }
                        if(row[3]!="1000-01-01 00:00:00"&& row[4]=="1000-01-01 00:00:00"){
                            val = row[3]+'~'
                        }
                        if(row[4]!="1000-01-01 00:00:00"&& row[3]=="1000-01-01 00:00:00"){
                            val ='~'+row[4]
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[5] == "1000-01-01 00:00:00") {
                            val = ''
                        }else {
                            val =row[5]
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[6] == 1) {
                            val = '生效'
                        }else  if (row[6] == 2) {
                            val = '未生效'
                        }else  if (row[6] == 3) {
                            val = '已失效'
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 6
                },



                {
                    "render": function (data, type, row, meta) {
                        var  val =""
                        if (row[2] == 1) {
                            val = '<a class="ml-10" onclick="Pagedetails(\'引导页\',\'guidepage.html\',\''+row[8]+'\')">编辑</a><a class="ml-10" onclick="carouselSettingsdelete(\''+row[8]+'\')">删除</a>'
                        }else  if (row[2] == 2) {
                            val = '<a class="ml-10" onclick="Pagedetails2(\'启动页\',\'startupPage.html\',\''+row[8]+'\')">编辑</a><a class="ml-10" onclick="carouselSettingsdelete(\''+row[8]+'\')">删除</a>'
                        }else  if (row[2] == 3) {
                            val = '<a class="ml-10" onclick="Pagedetails3(\'广告页\',\'advertisingPage.html\',\''+row[8]+'\')">编辑</a><a class="ml-10" onclick="carouselSettingsdelete(\''+row[8]+'\')">删除</a>'
                        }
                        return val;
                    },
                    "targets":7
                },

            ]
        });
    };
}
//名称
function over(id) {
    $("#mouseBox"+id).css({
        display: "inline"
    });
}
function out(id) {
    $("#mouseBox"+id).css({
        display: "none"
    });
}
/*跳转引导页防止多窗口打开*/
function Pagedetails(title,url,data){
    sessionStorage.setItem("guidePageID",data);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
/*跳转启动页防止多窗口打开*/
function Pagedetails2(title,url,data){
    sessionStorage.setItem("startupPageID",data);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
/*跳转启动页防止多窗口打开*/
function Pagedetails3(title,url,data){
    sessionStorage.setItem("advertisingPageID",data);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}


//删除
function carouselSettingsdelete(val) {
    var cu = layer.confirm('<span>确定要删除吗？</span>', {title:'提示',btn: ['确认', '取消']}, function(index){
        $.ajax({
            url: root.appversioncontrol.deleteAppScreenMsg,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            contentType: "application/json",
            data:JSON.stringify({
                "id":val,
            }),
            success: function (data) {
                if(data.errorCode==0){
                    $.Huimodalalert('删除成功',2000);
                    queryopenScreen(comPageNum)
                }else {
                    $.Huimodalalert(data.msg,2000);
                }

            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
        layer.close(cu);
    },function(){
        layer.close(cu);

    });
}




